# 存储公共的模型属性

from django.db import models
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from rest_framework.generics import ListAPIView


class BaseModel(models.Model):
    name = models.CharField(max_length=20, verbose_name='名称')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    is_deleted = models.BooleanField(default=False, verbose_name='删除标记')
    is_show= models.BooleanField(default=True, verbose_name='是否显示')
    orders=models.IntegerField(default=0,verbose_name='排序')

    class Meta:
        abstract = True  # 抽象模型类，不会在数据库中创建表
    def __str__(self):
        return self.name

# 缓存视图数据
class CacheView(ListAPIView):
    # 参数是缓存的时间，单位秒
    # 在原有的get方法上添加装饰器，实现缓存功能
    # cache_page
    @method_decorator(cache_page(60*60*24))
    def get(self, request, *args, **kwargs):
        return super().get(request, *args, **kwargs)

